//
//  HMEmoticonCollectionViewCell.swift
//  Weibo22
//
//  Created by Apple on 16/12/10.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
//  表情视图自定义cell
class HMEmoticonCollectionViewCell: UICollectionViewCell {
    
    //  MARK: -- 懒加载
    private lazy var messageLabel: UILabel = {
        let label = UILabel(fontSize: 20, textColor: UIColor.purple)
        return label
    }()
    
    //  记录20个表情按钮
    private lazy var emoticonButonArray: [HMEmoticonButton] = [HMEmoticonButton]()
    
    //  表情数组
    var emoticonArray: [HMEmoticon]? {
        didSet {
        
            guard let currentEmoticonArray = emoticonArray else {
                return
            }
            
            for button in emoticonButonArray {
                //  默认表情按钮隐藏
                button.isHidden = true
            }
            
            
            for (i, emoticon) in currentEmoticonArray.enumerated() {
                //  根据表情元素的下标获取表情按钮
                let button = emoticonButonArray[i]
                //  设置表情模型
                button.emoticon = emoticon
                //  绑定表情数据的时候让其显示
                button.isHidden = false
                if emoticon.type == "0" {
                    //  图片表情 -> 如果加载bundle里面的图片,需要拼接图片对应的bundle文件的路径
                    button.setImage(UIImage(named: emoticon.path!), for: .normal)
                    button.setTitle(nil, for: .normal)
                } else {
                    //  emoji表情
                    button.setTitle((emoticon.code! as NSString).emoji(), for: .normal)
                    button.setImage(nil, for: .normal)
                    
                }
                
                
            }
            
            
            
            
        }
    }
    
    var indexPath: IndexPath? {
        
        didSet {
            
            guard let currentIndexPath = indexPath else {
                return
            }
            
            messageLabel.text = "当前滚动的是第\(currentIndexPath.section + 1)组第\(currentIndexPath.item + 1)页"
            
            
        }
    
    }
    
    //  删除表情按钮
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        addChildButton()
        contentView.addSubview(deleteButton)
        
//        contentView.addSubview(messageLabel)
//        messageLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(contentView)
//        }
        
    }
    
    //  添加20个表情按钮
    
    private func addChildButton() {
    
        for _ in 0..<20 {
            let button = HMEmoticonButton()
            //  添加点击事件
            button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 34)
            contentView.addSubview(button)
            emoticonButonArray.append(button)
        }
        
        
    }
    //  MARK: -- 点击删除表情按钮的事件
    @objc private func deleteButtonAction() {
    
        //  发送删除表情的内容的通知
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmoticonNotificationName), object: nil)
        
        
    }
    
    //  MARK: -- 点击表情按钮的事件
    @objc private func btnAction(btn: HMEmoticonButton) {
        
        //  表情模型
        let emoticon = btn.emoticon!
        //  发送通知
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedEmoitconNotificationName), object: emoticon)
        
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //  表情按钮的宽度
        let buttonWith = width / 7
        //  表情按钮的高度
        let buttonHeight = height / 3
        
        for (i, button) in emoticonButonArray.enumerated() {
            
            //  计算列的索引
            let colIndex = i % 7
            //  计算行的索引
            let rowIndex = i / 7
            
            
            //  设置大小
            button.size = CGSize(width: buttonWith, height: buttonHeight)
            //  设置x,y坐标
            button.x = CGFloat(colIndex) * buttonWith
            button.y = CGFloat(rowIndex) * buttonHeight
            
        }
        //  设置删除表情按钮的大小
        deleteButton.size =  CGSize(width: buttonWith, height: buttonHeight)
        //  设置删除按钮的x,y坐标
        deleteButton.x = width - buttonWith
        deleteButton.y = height - buttonHeight
        
        
    }
    
    
    
    
    
    
    
    
}
